/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GamePlay;

import FrameWork.*;
import FrameWork.Audio.RSAudio;
import java.io.IOException;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.Sprite;
/**
 *
 * @author DANTRUONG
 */
public class State_Menu extends Virtual_Menu{
    static final int ID_PLAY         = 0;
    static final int ID_OPTION       = 1;
    static final int ID_HELP         = 2;
    static final int ID_ABOUT        = 3;
    static final int ID_EXIT         = 4;
    
    int m_Status;
    Point [] m_CurPos = new Point[5];
    Sprite m_Menu;
    Sprite m_Bub;
    Sprite m_Ball;
    Rectangle[] m_Rect = new Rectangle[5];
    int m_IDChoosed;
    
    public State_Menu (IPlay _Play){
        super(_Play);    
        m_IDState = Global.STATE_MENU ;
        m_IDChoosed = 0;
    }   
   
    public void Init (){
        super.Init();
        try {
            m_Menu = new  Sprite (Image.createImage("/Image/menu.png"),
                                148, 200);
            m_Bub = new Sprite (Image.createImage("/Image/bubble.png"),
                            90, 90);
            m_Ball = new Sprite (Image.createImage("/Image/ball.png"),
                            60, 60);
        } catch (IOException ex) {
            ex.printStackTrace();
        }
       
       
        m_CurPos[0] = new Point(100 + 88, 50);
        m_CurPos[1] = new Point(33 - 88, 110);
        
        m_CurPos[2] = new Point(116 + 88, 165);
//        m_CurPos[2] = new Point(100 + 88, 150);
        
        
        m_CurPos[3] = new Point(51 - 88, 201);
        m_CurPos[4] = new Point(108 + 88, 236);
        m_Rect[0]= new Rectangle(100, 50, 90, 90);
        m_Rect[1]= new Rectangle(33, 110, 90, 90);
        
        m_Rect[2]= new Rectangle(116, 165, 60, 60);
//        m_Rect[2]= new Rectangle(100, 150, 90, 90);
        
        m_Rect[3]= new Rectangle(51, 199, 60, 60);
        m_Rect[4]= new Rectangle(108, 236, 60, 60);
        m_Status = 0;
        RSAudio.getRS().PlayBack();
    }
    public void Update (float _Time){
        switch(m_Status){
            case 0:
                if(m_CurPos[0].X > 100){
                        m_CurPos[0] = new Point(m_CurPos[0].X - 8, m_CurPos[0].Y);
                        m_CurPos[2] = new Point(m_CurPos[2].X - 8, m_CurPos[2].Y);
                        m_CurPos[4] = new Point(m_CurPos[4].X - 8, m_CurPos[4].Y);
                }
                if(m_CurPos[0].X < 100){
                        m_CurPos[0] = new Point( 100, m_CurPos[0].Y);
                        m_CurPos[2] = new Point(m_CurPos[2].X + (100 - m_CurPos[0].X), m_CurPos[2].Y);
                        m_CurPos[4] = new Point(m_CurPos[4].X + (100 - m_CurPos[0].X), m_CurPos[4].Y);
                }
                if(m_CurPos[1].X < 33){
                    m_CurPos[1] = new Point(m_CurPos[1].X + 8, m_CurPos[1].Y);
                    m_CurPos[3] = new Point(m_CurPos[3].X + 8, m_CurPos[3].Y);
                }
                if(m_CurPos[1].X > 33){
                    m_CurPos[1] = new Point(33, m_CurPos[1].Y);
                    m_CurPos[3] = new Point(m_CurPos[3].X - (m_CurPos[1].X - 33), m_CurPos[3].Y);
                }
                if(m_CurPos[0].X == 100 && m_CurPos[1].X == 33)
                    m_Status = 1;
                break;
            case 2:
                if(m_CurPos[0].X < 100 + 88){
                        m_CurPos[0] = new Point(m_CurPos[0].X + 8, m_CurPos[0].Y);
                        m_CurPos[2] = new Point(m_CurPos[2].X + 8, m_CurPos[2].Y);
                        m_CurPos[4] = new Point(m_CurPos[4].X + 8, m_CurPos[4].Y);
                }
                if(m_CurPos[0].X > 100 + 88){
                        m_CurPos[0] = new Point( 188, m_CurPos[0].Y);
                        m_CurPos[2] = new Point(m_CurPos[2].X - (m_CurPos[0].X - 188), m_CurPos[2].Y);
                        m_CurPos[4] = new Point(m_CurPos[4].X - (m_CurPos[0].X - 188), m_CurPos[4].Y);
                }
                if(m_CurPos[1].X > 33 - 88){
                    m_CurPos[1] = new Point(m_CurPos[1].X - 8, m_CurPos[1].Y);
                    m_CurPos[3] = new Point(m_CurPos[3].X - 8, m_CurPos[3].Y);
                }
                if(m_CurPos[1].X < 33 - 88){
                    m_CurPos[1] = new Point(33, m_CurPos[1].Y);
                    m_CurPos[3] = new Point(m_CurPos[3].X + (-55 - m_CurPos[1].X), m_CurPos[3].Y);
                }
                if(m_CurPos[0].X == 188 && m_CurPos[1].X == -55){
                    switch(m_IDChoosed){
                      case ID_PLAY:
                          m_Play.setNextState(
                          new State_Level(m_Play));
                          break;
                      case ID_OPTION:
                          m_Play.setNextState(
                          new State_Option(m_Play));
                          break;
                      case ID_HELP:
                           m_Play.setNextState(
                           new State_Help(m_Play));
                           break;
                      case ID_ABOUT:
                          m_Play.setNextState(
                          new State_About(m_Play));
                          break;
                      case ID_EXIT:
                          m_Play.setNextState(
                          new particle(m_Play));
                          break; 
                    }
                }
                break;
                
        }
    }
    public void PointerPressed(int x, int y){
        if(m_Status == 1){
            for(int i = 0; i<5; i++){
                if(m_Rect[i].Intersects(new Point(x,y))){
                    m_IDChoosed = i;
                    RSAudio.getRS().PlayExplosion();
                    m_Status = 2;
                }
            }
        }
    }
    public void KeyPressed (int _Keycode){
        if(m_Status == 1){
            if (_Keycode == -1 || _Keycode == 50){
                if(m_IDChoosed != 0)
                    m_IDChoosed -= 1;
                else m_IDChoosed = 4;
                RSAudio.getRS().PlayBubble();
            }  
            if (_Keycode == -2 || _Keycode == 56){
                if(m_IDChoosed != 4)
                    m_IDChoosed += 1;
                else m_IDChoosed = 0;
                RSAudio.getRS().PlayBubble();
            }

            if (_Keycode == -5 || _Keycode == 53){
                RSAudio.getRS().PlayExplosion();
                  switch(m_IDChoosed){
                      case ID_PLAY:
                      case ID_OPTION:
                      case ID_HELP:
                      case ID_ABOUT:
                      case ID_EXIT:
                          m_Status = 2;
                          break; 
                  }
            }
        }
    }
    
    public void Render (){
       super.Render();
       if(MainCanvas.Instance().mGame.getWidth()>
               MainCanvas.Instance().mGame.getHeight()){
           RenderLandScape();
       }
       m_Menu.setFrame(0);
       m_Menu.setPosition(37, 79);
       m_Menu.paint(m_Graphics);
       
       m_Bub.setFrame(0);
       m_Bub.setPosition(m_CurPos[0].X, m_CurPos[0].Y);
       m_Bub.paint(m_Graphics);
       
       m_Bub.setFrame(0);
       m_Bub.setPosition(m_CurPos[1].X, m_CurPos[1].Y);
       m_Bub.paint(m_Graphics);
       
       m_Ball.setFrame(0);
       m_Ball.setPosition(m_CurPos[2].X, m_CurPos[2].Y);
       m_Ball.paint(m_Graphics);
       
//        m_Bub.setFrame(0);
//       m_Bub.setPosition(m_CurPos[2].X, m_CurPos[2].Y);
//       m_Bub.paint(m_Graphics);
       
       m_Ball.setFrame(0);
       m_Ball.setPosition(m_CurPos[3].X, m_CurPos[3].Y);
       m_Ball.paint(m_Graphics);
       
       m_Ball.setFrame(0);
       m_Ball.setPosition(m_CurPos[4].X, m_CurPos[4].Y);
       m_Ball.paint(m_Graphics);
       if(m_Status == 1){
           switch(m_IDChoosed){
                      case ID_PLAY:
                          m_Bub.setFrame(1);
                          m_Bub.setPosition(100, 50);
                          m_Bub.paint(m_Graphics);
                          break;
                      case ID_OPTION:
                          m_Bub.setFrame(1);
                          m_Bub.setPosition(33, 110);
                          m_Bub.paint(m_Graphics);
                          break;
                      case ID_HELP:
//                          m_Bub.setFrame(1);
//                          m_Bub.setPosition(100, 150);
//                          m_Bub.paint(m_Graphics);
                          m_Ball.setFrame(1);
                          m_Ball.setPosition(116, 165);
                          m_Ball.paint(m_Graphics);
                          break;
                      case ID_ABOUT:
                          m_Ball.setFrame(1);
                          m_Ball.setPosition(51, 201);
                          m_Ball.paint(m_Graphics);
                          break;
                      case ID_EXIT:
                          m_Ball.setFrame(1);
                          m_Ball.setPosition(108, 236);
                          m_Ball.paint(m_Graphics);
                          break; 
                  }
       }
    }
    public void Destroy (){
        m_Menu =null;
        m_Bub = null;
        m_Ball = null;
    }
}
